(function() {
  var __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  define(['views/base/view', 'jquery.ui.widget', 'jquery.fileupload', 'jquery.iframe-transport', 'text!templates/base/file.html'], function() {
    'use strict';
    var BaseView, FileView, Template, _ref;
    BaseView = require('views/base/view');
    Template = require('text!templates/base/file.html');
    return FileView = (function(_super) {
      __extends(FileView, _super);

      function FileView() {
        _ref = FileView.__super__.constructor.apply(this, arguments);
        return _ref;
      }

      FileView.prototype.template = Template;

      FileView.prototype.className = 'cadastro-file';

      FileView.prototype.events = {
        'click .file-remove': 'clickFileRemoveHandler'
      };

      FileView.prototype.initialize = function() {
        FileView.__super__.initialize.apply(this, arguments);
        this.label || (this.label = 'Label do arquivo');
        this.fileId || (this.fileId = 'input-' + this.cid);
        this.uploadUrl || (this.uploadUrl = window.AppData.API_URL + '/upload');
        this.imagePath || (this.imagePath = window.AppData.API_URL + '/arquivo/');
        this.maxSize || (this.maxSize = 3000000);
        this.acceptFilesRegex || (this.acceptFilesRegex = /(\.|\/)(shp|dbf|shx|kml)$/i);
        this.acceptFileArray || (this.acceptFileArray = ['.shp', '.dbf', '.shx', '.kml']);
        return this.render();
      };

      FileView.prototype.getTemplateData = function() {
        return _.extend(FileView.__super__.getTemplateData.apply(this, arguments), {
          label: this.label,
          fileId: this.fileId,
          fileTypes: this.acceptFileArray.join(',')
        });
      };

      FileView.prototype.afterRender = function() {
        FileView.__super__.afterRender.apply(this, arguments);
        return this.setupFileInput();
      };

      FileView.prototype.cacheDOMElements = function() {
        return _.extend(FileView.__super__.cacheDOMElements.apply(this, arguments), {
          $formGroup: this.$('.form-group'),
          $fileInput: this.$('#' + this.fileId),
          $inputGroup: this.$('.input-group'),
          $fileLink: this.$('.file-link'),
          $loaderContainer: this.$('.loader-container'),
          $loaderColumn: this.$('.loader-column')
        });
      };

      FileView.prototype.setupFileInput = function(options) {
        var defaultOptions,
          _this = this;
        if (options == null) {
          options = {};
        }
        defaultOptions = {
          dataType: 'text',
          url: this.uploadUrl,
          forceIframeTransport: true,
          limitMultiFileUploadSize: this.maxSize,
          acceptFileTypes: this.acceptFilesRegex,
          fail: function() {
            return _this.publishEvent('feedback:error', 'upload.erro');
          },
          add: function(event, data) {
            var file;
            _this.closeTooltips();
            data.files = _.filter(data.files, function(f) {
              return _this.acceptFilesRegex.test(f.name);
            });
            if (data.files.length) {
              file = data.files[0];
            }
            if (!file) {
              _this.publishEvent('feedback:error', 'upload.erro.tipo');
              return;
            }
            if (file.size && file.size > _this.maxSize) {
              _this.publishEvent('feedback:error', 'upload.erro.tamanho');
              return;
            }
            _this.setLinkText(file.name);
            _this.showLoader();
            return data.submit();
          },
          done: function(event, data) {
            return _this.setFileSrc(data.result);
          }
        };
        return this.DOMElements.$fileInput.fileupload(_.extend(defaultOptions, options));
      };

      FileView.prototype.showLoader = function() {
        this.DOMElements.$inputGroup.addClass('hide');
        this.DOMElements.$loaderColumn.removeClass('hide');
        return this.DOMElements.$loaderContainer.removeClass('hide');
      };

      FileView.prototype.hideLoader = function() {
        this.DOMElements.$formGroup.removeClass('has-success .has-error');
        this.DOMElements.$loaderContainer.addClass('hide');
        this.DOMElements.$inputGroup.removeClass('hide');
        this.DOMElements.$loaderColumn.removeClass('hide');
        return this.DOMElements.$fileLink.attr('href', '').text('');
      };

      FileView.prototype.setLinkText = function(text) {
        if (!text) {
          return;
        }
        return this.DOMElements.$fileLink.text(text);
      };

      FileView.prototype.setFileSrc = function(fileName) {
        var path;
        if (!fileName) {
          return;
        }
        if (!this.DOMElements.$fileLink.text()) {
          this.setLinkText(fileName);
        }
        path = this.imagePath;
        this.DOMElements.$fileLink.attr('href', path + fileName);
        this.showLoader();
        this.DOMElements.$loaderColumn.addClass('hide');
        this.fileName = fileName;
        this.trigger('file:edited', this.fileName);
        return this.fileName;
      };

      FileView.prototype.removeFile = function() {
        this.hideLoader();
        this.fileName = null;
        return this.trigger('file:removed');
      };

      FileView.prototype.getFileName = function() {
        return this.fileName;
      };

      FileView.prototype.clickFileRemoveHandler = function(event) {
        event.preventDefault();
        return this.removeFile();
      };

      return FileView;

    })(BaseView);
  });

}).call(this);
